package com.cyyang.shop.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.cyyang.shop.bean.Spec;
import com.cyyang.shop.bean.SpecOption;
import com.cyyang.shop.mapper.SpecMapper;
import com.cyyang.shop.service.SpecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service(timeout = 10000)
public class SpecServiceImpl implements SpecService{

	@Autowired
	SpecMapper specMapper; 

	@Override
	public PageInfo<Spec> list(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Spec> list = specMapper.list();
		return new PageInfo<Spec>(list);
	}

	@Override
	public void delBatch(String ids) {
		specMapper.del(ids);
	}

	@Override
	public void add(Spec spec) {
		specMapper.add(spec);
		System.out.println("----------------------");
		System.out.println(spec);
		if (spec.getOptionList()!=null  && spec!=null && spec.getOptionList().size()>0) {
			List<SpecOption> list = spec.getOptionList();
			list.forEach((option) ->{
				if (option!=null && option.getOptionName()!=null && option.getOptionName()!="") {
						option.setSpecId(spec.getId());
						specMapper.addOption(option);
					}
				}
			);
		}
		
		
	}

	@Override
	public Spec findSpecBySpecId(Integer specId) {
		// TODO Auto-generated method stub
		return specMapper.findSpecBySpecId(specId);
	}

	@Override
	public void update(Spec spec) {
		specMapper.update(spec);
		specMapper.deleteOptionsBySpecId(spec.getId());
		// 其次添加的是hg_spec_option
		if (spec != null && spec.getOptionList() != null && spec.getOptionList().size() > 0) {
			List<SpecOption> optionList = spec.getOptionList();
			optionList.forEach((option) -> {
				if (option != null && option.getOptionName() != null && option.getOptionName() != "") {
					option.setSpecId(spec.getId());
					specMapper.addOption(option);
				}
			});
		}
		
	}

	@Override
	public List<SpecOption> findOptionsBySpecId(int specId) {
		return specMapper.findOptionsBySpecId(specId);
	}

	@Override
	public List<Spec> findSpecsBySkuId(Integer id) {
		return specMapper.findSpecsBySkuId(id);
	}

	@Override
	public List<SpecOption> findSpecOptionBySpecId(Integer specId) {
		return specMapper.findSpecOptionBySpecId(specId);
	}


}
